package readsourcecode.ch2;

import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Date 2022/8/7
 * @Author Jonathan
 */
public class PostProcessorContainer<T> {
    private Class<BasePostProcessor> monitorPostProcessorClass;

    private PostProcessorContainer() {

    }

    public static <T> PostProcessorContainer getInstance(Class<T> monitorPostProcessorClass) {
        PostProcessorContainer postProcessorContainer = new PostProcessorContainer();
        postProcessorContainer.monitorPostProcessorClass = monitorPostProcessorClass;
        return postProcessorContainer;
    }


    public boolean handleBefore(PostContext<T> postContext) {
        List<? extends BasePostProcessor> postProcessors = getMyClass();
        if (CollectionUtils.isEmpty(postProcessors)) {
            return true;
        }

        //排序
        postProcessors.sort(new ProcessorComparator());
        for (BasePostProcessor postProcessor : postProcessors) {
            postProcessor.handleBefore(postContext);
        }
        return false;
    }


    public void handleAfter(PostContext<T> postContext) {
        final List<? extends BasePostProcessor> postProcessors = getMyClass();
        if (CollectionUtils.isEmpty(postProcessors)) {
            return;
        }
        // 排序 优先级越高 越靠近内核
        postProcessors.sort(new ProcessorComparator());
        for (BasePostProcessor postProcessor : postProcessors) {
            postProcessor.handleAfter(postContext);
        }
    }

    private List<? extends BasePostProcessor> getMyClass() {
        return null;
    }
}
